package com.excellent.cloudApi;

import com.excellent.entity.ChatResponse;
import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;


/**
 * 需要添加依赖
 * <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
 * <dependency>
 * <groupId>com.squareup.okhttp3</groupId>
 * <artifactId>okhttp</artifactId>
 * <version>4.12.0</version>
 * </dependency>
 */

public class Chat {

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static ChatResponse getAnswer(String appId, String conversationId, String question) throws IOException, JSONException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, String.format("{\"app_id\":\"%s\",\"conversation_id\":\"%s\",\"query\":\"%s\",\"stream\":false}", appId, conversationId, question));
        Request request = new Request.Builder()
                .url("https://qianfan.baidubce.com/v2/app/conversation/runs")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Appbuilder-Authorization", "Bearer bce-v3/ALTAK-jnYex1wotKizobcUiYg8Z/ab021ae321c579d5ea3335f2eac029995a23de3e")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
//        System.out.println(response.body().string());
        JSONObject jsonObject = new JSONObject(response.body().string());
        return new ChatResponse(jsonObject);
    }


}